/* Created By TheBrodieMan */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using CommonBehaviors.Actions;
using System.Windows.Forms;
using System.Windows.Media;
using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Profiles;
using Styx.Pathing;
using Styx.TreeSharp;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

using Honorbuddy.QuestBehaviorCore;

using Action = Styx.TreeSharp.Action;

namespace Honorbuddy.Quest_Behaviors.SpecificQuests.SegmentationFault
{
	[CustomBehaviorFileName(@"SpecificQuests\Kalimdor\Azshara\14310-SegmentationFault")]
	public class SegmentationFault : CustomForcedBehavior
	{
		public SegmentationFault(Dictionary<string, string> args)
			: base(args)
		{
			try
			{
				QuestId = 14310;//GetAttributeAsQuestId("QuestId", true, null) ?? 0;
			}
			catch
			{
				Logging.Write("Problem parsing a QuestId in behavior: Segmentation Fault: Core Dumped");
			}
		}
		public int QuestId { get; set; }
		private bool _isBehaviorDone;
		public int MobIdAzsharite = 36105;
		public int StartSwitch = 195683;
		public WoWPoint LocationSwitch = new WoWPoint(2538.287, -5786.184, 76.73697);
		private Composite _root;
		public QuestCompleteRequirement questCompleteRequirement = QuestCompleteRequirement.NotComplete;
		public QuestInLogRequirement questInLogRequirement = QuestInLogRequirement.InLog;
		public static void BPdiag(string message, params object[] args) { Logging.WriteDiagnostic(Colors.LawnGreen, "[QB DIAG TBM: " + message, args); }

		public override bool IsDone
		{
			get { return _isBehaviorDone; }
		}
		
		private LocalPlayer Me
		{
			get { return (StyxWoW.Me); }
		}

		public override void OnStart()
		{
			OnStart_HandleAttributeProblem();
			if (!IsDone)
			{
				if (TreeRoot.Current != null && TreeRoot.Current.Root != null && TreeRoot.Current.Root.LastStatus != RunStatus.Running)
				{
					var currentRoot = TreeRoot.Current.Root;
					if (currentRoot is GroupComposite)
					{
						var root = (GroupComposite)currentRoot;
						root.InsertChild(0, CreateBehavior());
					}
				}
				PlayerQuest Quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
				TreeRoot.GoalText = ((Quest != null) ? ("\"" + Quest.Name + "\"") : "In Progress");
			}
		}

		public WoWUnit Azsharite
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWUnit>().Where
					(u => (u.Entry == MobIdAzsharite) && u.IsAlive && u.Distance < 60).FirstOrDefault();
			}
		}
		
		public WoWObject Switch
		{
			get
			{
				return ObjectManager.GetObjectsOfType<WoWObject>().Where
					(u => (u.Entry == StartSwitch) && u.Distance < 60).FirstOrDefault();
			}
		}

		public bool IsQuestComplete()
		{
			var quest = StyxWoW.Me.QuestLog.GetQuestById((uint)QuestId);
			return quest == null || quest.IsCompleted;
		}
		
		public bool ItemOnCooldown(uint ItemId)
		{
			return Lua.GetReturnVal<bool>("GetItemCooldown(" + ItemId + ")", 0);
		}
		
		public void UseQuestItem(uint ItemId)
		{
			Lua.DoString("UseItemByName(" + ItemId + ")");
		}

		public void UseIfNotOnCooldown(uint ItemId)
		{
			if (!ItemOnCooldown(ItemId))
			{
				UseQuestItem(ItemId);
			}
		}

		public Composite DoneYet
		{
			get
			{
				return
					new Decorator(ret => IsQuestComplete(), new Action(delegate
					{
						TreeRoot.StatusText = "Finished!";
						_isBehaviorDone = true;
						return RunStatus.Success;
					}));
			}
		}

		public Composite SprayAzsharite
		{
			get
			{
				return new PrioritySelector(
					new Decorator(context => Azsharite != null,
						new PrioritySelector(
							new Decorator(context => Me.IsMounted() || Me.IsShapeshifted(),
								new UtilityBehaviorPS.ExecuteMountStrategy(
									context => MountStrategyType.DismountOrCancelShapeshift,
									context => NavType.Fly)),
							new Decorator(context => Me.Location.Distance(Azsharite.Location) > 5,
								new Action(context => 
								{
									Navigator.MoveTo(Azsharite.Location);
								})),
							new Decorator(context => Me.Location.Distance(Azsharite.Location) < 15,
								new Action(context =>
								{
									Azsharite.Target();
									Azsharite.Face();
									WoWMovement.MoveStop();
									UseIfNotOnCooldown(49132);
									Thread.Sleep(1000);
								})))));
			}
		}

		protected override Composite CreateBehavior()
		{
			return _root ?? (_root = new Decorator(ret => !_isBehaviorDone,
				new PrioritySelector(
					new Decorator(context => Azsharite == null,
						new Action(context => { _isBehaviorDone = true; })),
					DoneYet,
					SprayAzsharite,
					new ActionAlwaysSucceed())));
		}
	}
}